package com.rongc.client.freight.service;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.model.DicsBean;
import com.rongc.client.freight.base.request.api.DicsApi;

/**
 * Created by Bruce on 2016/11/3.
 */
public class DicSyncService extends IntentService{

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public DicSyncService(String name) {
        super(name);
    }
    public DicSyncService() {
        super("DicSyncService");
    }
    @Override
       protected void onHandleIntent(Intent intent) {
        RequestManager.getInstance().call(new DicsApi(new DicsApi.DicsParams(BaseDic.DicsType.LENGTH_TYPE),
                respLengthListener, errorListener));

        RequestManager.getInstance().call(new DicsApi(new DicsApi.DicsParams(BaseDic.DicsType.MODEL_TYPE),
                respModelListener, errorListener));


    }

    Response.Listener<DicsBean.DicBean[]> respLengthListener = new Response.Listener<DicsBean.DicBean[]>() {

        @Override
        public void onResponse(DicsBean.DicBean[] response) {
            if (HandleCode.requestSuccess()) {
                if(response != null) {
                    DicsBean dics = new DicsBean();
//                    dics.clear(BaseDic.DicsType.LENGTH_TYPE);
                    for (int i = 0; i < response.length; i++) {
                        dics.getDics().add(response[i]);
                    }
                    dics.save(BaseDic.DicsType.LENGTH_TYPE);
                }

            }
        }
    };

    Response.Listener<DicsBean.DicBean[]> respModelListener = new Response.Listener<DicsBean.DicBean[]>() {

        @Override
        public void onResponse(DicsBean.DicBean[] response) {
            if (HandleCode.requestSuccess()) {
                if(response != null) {
                    DicsBean dics = new DicsBean();
//                    dics.clear(BaseDic.DicsType.LENGTH_TYPE);
                    for (int i = 0; i < response.length; i++) {
                        dics.getDics().add(response[i]);
                    }
                    dics.save(BaseDic.DicsType.MODEL_TYPE);
                }
            }
        }
    };



    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };
}
